package com.qjl.shopping.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjl.shopping.bean.Admin;
import com.qjl.shopping.common.ResponseEntity;
import com.qjl.shopping.controller.vm.AdminVM;
import com.qjl.shopping.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author liguanghui
 * @Date 2023/5/1
 */
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;
    @GetMapping("/find/admin/{id}")
    public ResponseEntity findAdminById(@PathVariable int id){
        Admin byId = adminService.getById(id);
        if(byId==null){
            return ResponseEntity.error("无该管理员");
        }
        return ResponseEntity.success("根据id查询管理员成功",byId);
    }
    @GetMapping("/find/admin/name")
    public ResponseEntity findAdminByName(@RequestParam String name){

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_name",name);
        Admin byId = adminService.getOne(queryWrapper);
        if(byId==null){
            return ResponseEntity.error("无该管理员");
        }
        AdminVM adminVM = new AdminVM();
        BeanUtils.copyProperties(byId,adminVM);
        return ResponseEntity.success("根据id查询管理员成功",adminVM);
    }

    //查询全部
    @GetMapping("/find/all/admins")
    public ResponseEntity findAdmins(@RequestParam(defaultValue = "0") int pageNum,
                                     @RequestParam(defaultValue = "9") int pageSize){
        Page<Admin> page = adminService.selectPage(pageNum, pageSize);
        return ResponseEntity.success("查询全部管理员成功",page);
    }

    //    登录
    @GetMapping("/login")
    public ResponseEntity login(String ac,String pwd){
        AdminVM a = adminService.login(ac,pwd);
        return ResponseEntity.success("登录成功", a);
    }

//    //修改管理员本账号的信息（不包括密码）
//    @PostMapping("/update/admin/person")
//    public ResponseEntity updateAdmin(@RequestBody Admin admin){
//        adminService.updateAdminByName(admin);
//        return ResponseEntity.success("修改管理员信息成功");
//    }

    //修改管理员密码
//    @PostMapping("/update/admin/password")
//    public ResponseEntity updateAdminPassword(@RequestBody Map<String, Object> list){
////        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();
////        System.out.println(userInfo.get("name"));
////        Object n = userInfo.get("name");
////        String name = String.valueOf(n);
//        String name =(String) list.get("name");
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("admin_name",name);
//        Admin admin1 = adminService.getOne(queryWrapper);
//        String password =(String) list.get("password");
//        String newPassword =(String) list.get("newPassword");
////        System.out.println(password+"00000000000000"+admin1.getPassword()+"000000"+newPassword);
//        if(!admin1.getPassword().equals(password)){
//            throw new ServiceException(Constants.CODE_401,"原密码错误");
//        }
//        admin1.setPassword(newPassword);
//        adminService.updateById(admin1);
//
//        return ResponseEntity.success("修改管理员密码成功");
//    }
}
